package com.lnsf.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.lnsf.entity.Department;
import com.lnsf.entity.Employee;
import com.lnsf.service.DeptService;
import com.lnsf.service.EmpService;

@Controller
public class EmpController {
	
	@Autowired
	private EmpService empService;
	
	@Autowired
	private DeptService deptService;
	
	
	@ModelAttribute("employee1")
	public Employee getEmployee(Employee emp) {
		if(emp!=null && emp.getEmpNo()>0){
			Department department = deptService.getDepartmentById(emp.getDepartment().getDeptNo());
			emp.setDepartment(department);
		}
		return emp;
	}
	
	
	/*
	 * 获得全部Emp
	 * 
	 */
	@RequestMapping("/getAllEmployee")
	public ModelAndView getAllEmployee() {
		List<Employee> employees = empService.getAllEmployee();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("employees", employees);
		modelAndView.setViewName("success");
		
		return modelAndView;
	}
	
	/*Ajax查询*/
	@RequestMapping("/getAllEmployeeByAjax")
	@ResponseBody
	public List<Employee> getAllEmployeeByAjax() {
		//System.out.println("aaa");
		List<Employee> employees = empService.getAllEmployee();
	
		return employees;
	}
	
	/*
	 * 根据id查找emp
	 * List
	 */
	@RequestMapping("/getEmployeeById/{id}")
	public ModelAndView getEmployeeById(@PathVariable("id")int empno) {
		
		List<Employee> list = new ArrayList<Employee>();
		ModelAndView modelAndView = new ModelAndView();
		Employee employee = empService.getEmployeeById(empno);
		list.add(employee);
		System.out.println(list);
		modelAndView.addObject("employees", list);
		modelAndView.setViewName("success");
		
		return modelAndView;
	}
	
	
	/*
	 * 修改先获取
	 */
	@RequestMapping("/toDetails/{id}")
	public String toDetails(@PathVariable("id") int empno , Map<String, Object> map){
		Employee employee1 = new Employee();
		employee1 = empService.getEmployeeById(empno);	
		System.out.println("获取"+employee1);
		List<Department> departments = deptService.getAllDepartment();
		
		map.put("employeeInfo", employee1);
		map.put("departments", departments);
		return "EmpDetails";
	}	
	
	/*
	 * 增加先获取
	 * */
	@RequestMapping("/toAddEmp")
	public String toAddEmp(Map<String, Object> map){
		List<Department> departments = deptService.getAllDepartment();
		map.put("departments", departments);
		return "addEmployee";
	}
	
	
	/*
	 * 增加，更新
	 */
	@RequestMapping("/saveEmployee")
	public ModelAndView saveEmployee(Employee emp) {
		ModelAndView modelAndView = new ModelAndView();
		if(emp.getEmpNo()>0){
			empService.updateEmployee(emp);
		}else{
			emp.setEmpNo(empService.getCurrentEmpNo());
			empService.addEmployee(emp);
		}
		
		modelAndView.setView(new RedirectView("getAllEmployee"));
		
		return modelAndView;
	}
	
	@RequestMapping(value="/saveEmployeeByAjax",method=RequestMethod.POST)
	@ResponseBody
	public String saveEmployeeByAjax(@RequestBody Employee emp) {
		System.out.println(emp);
		if(emp.getEmpNo()>0){
			int i = empService.updateEmployee(emp);
			//System.out.println("aaaaaaaaaaaaaaaa"+emp.getEmpNo());
			if(i>0)
				return "update";
			else
				return "error";
			
		}else{
			emp.setEmpNo(empService.getCurrentEmpNo());
			int i = empService.addEmployee(emp);
			if(i>0)
				return "save";
			else
				return "error";
		}	
		
	}
	
	/*
	 * 删除
	 */
	@RequestMapping("/deleteEmpById/{id}")
	public String deleteEmployeeById(@PathVariable("id")int empno) {
		System.out.println(empno);
		empService.deleteEmployee(empno);
		return "redirect:/getAllEmployee";
	}
	
	
	
	
	
	
	
	public EmpService getEmpService() {
		return empService;
	}
	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}
}
